<template>
  <div class="edit-project-container">
    <el-dialog title="编辑项目"
               :visible.sync="visible"
               :close-on-click-modal='false'
               :close-on-press-escape="false"
               width="80%"
               @close="dialogClose">
      <el-form :model="ruleForm"
               status-icon
               :rules="rules"
               ref="ruleForm"
               label-width="120px"
               class="demo-ruleForm editProject-form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称"
                          prop="project_name">
              <el-input class="normal-input"
                        v-model="ruleForm.project_name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目简称"
                          prop="nick_name">
              <el-input class="normal-input"
                        v-model="ruleForm.nick_name"></el-input>
              <el-tooltip class="item"
                          effect="dark"
                          content="项目简称，方便手机显示项目名称"
                          placement="right">
                <el-button icon="el-icon-question"
                           type="text"></el-button>
              </el-tooltip>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务类型"
                          prop="business_type">
              <el-select v-model="ruleForm.business_type"
                         class="normal-input"
                         placeholder="选择业务类型">
                <el-option v-for="item in typeList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="外部系统编码"
                          prop="system_num">
              <el-input class="normal-input"
                        v-model="ruleForm.system_num"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目状态"
                          prop="pro_state">
              <el-select v-model="ruleForm.pro_state"
                         class="normal-input"
                         placeholder="项目状态">
                <el-option v-for="item in projectList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目许可编号"
                          prop="pro_num">
              <el-input class="normal-input"
                        v-model="ruleForm.pro_num"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="专业类型"
                          prop="profess_type">
              <el-select v-model="ruleForm.profess_type"
                         class="normal-input"
                         placeholder="专业类型">
                <el-option v-for="item in professList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
              <el-tooltip class="item"
                          effect="dark"
                          content="完成项目体系配置后进行操作"
                          placement="right">
                <el-button icon="el-icon-question"
                           type="text"></el-button>
              </el-tooltip>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建筑类型"
                          prop="build_type">
              <el-select v-model="ruleForm.build_type"
                         class="normal-input"
                         placeholder="选择建筑类型">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同总额(万元)"
                          prop="deal_total">
              <el-input class="normal-input"
                        v-model="ruleForm.deal_total"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="装配率(%)"
                          prop="deal_total">
              <el-input class="normal-input"
                        v-model="ruleForm.deal_total"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span='12'>
            <el-form-item label="签订时间"
                          prop="deal_total">
              <el-input class="normal-input"
                        v-model="ruleForm.deal_total"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="技术应用"
                          prop="deal_total">
              <el-input class="normal-input"
                        type="textarea"
                        v-model="ruleForm.deal_total"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设计单位"
                          prop="build_unit">
              <div class="build-unit"
                   v-for="(item,index) in buildUnitArr"
                   :key="index">
                <el-select v-model="item.build_unit"
                           class="normal-input add-input"
                           placeholder="选择设计单位">
                  <el-option v-for="sonItem in professList"
                             :key="sonItem.value"
                             :label="sonItem.label"
                             :value="sonItem.value">
                  </el-option>
                </el-select>
                <el-button type="primary"
                           icon="el-icon-plus"
                           @click="addUnit"
                           class="add-btn"
                           v-if="index==0?true:false">增加</el-button>
                <el-button v-else
                           icon="el-icon-minus"
                           type="danger"
                           class="add-btn"
                           @click="deleteUnit(index)">删除</el-button>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="施工单位"
                          prop="build_unit">
              <div class="build-unit"
                   v-for="(item,index) in buildUnitArr"
                   :key="index">
                <el-select v-model="item.build_unit"
                           class="normal-input add-input"
                           placeholder="选择施工单位">
                  <el-option v-for="sonItem in professList"
                             :key="sonItem.value"
                             :label="sonItem.label"
                             :value="sonItem.value">
                  </el-option>
                </el-select>
                <el-button type="primary"
                           icon="el-icon-plus"
                           @click="addUnit"
                           class="add-btn"
                           v-if="index==0?true:false">增加</el-button>
                <el-button v-else
                           icon="el-icon-minus"
                           type="danger"
                           class="add-btn"
                           @click="deleteUnit(index)">删除</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="构件加工厂"
                          prop="build_unit">
              <div class="build-unit"
                   v-for="(item,index) in buildUnitArr"
                   :key="index">
                <el-select v-model="item.build_unit"
                           class="normal-input add-input"
                           placeholder="选择构件加工厂">
                  <el-option v-for="sonItem in professList"
                             :key="sonItem.value"
                             :label="sonItem.label"
                             :value="sonItem.value">
                  </el-option>
                </el-select>
                <el-button type="primary"
                           icon="el-icon-plus"
                           @click="addUnit"
                           class="add-btn"
                           v-if="index==0?true:false">增加</el-button>
                <el-button v-else
                           icon="el-icon-minus"
                           type="danger"
                           class="add-btn"
                           @click="deleteUnit(index)">删除</el-button>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目地址"
                          prop="pro_manager">
              <el-select v-model="ruleForm.pro_manager"
                         class="area-input"
                         placeholder="省">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
              <el-select v-model="ruleForm.pro_manager"
                         class="area-input"
                         placeholder="市">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
              <el-select v-model="ruleForm.pro_manager"
                         class="area-input"
                         placeholder="区">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目经理"
                          prop="pro_manager">
              <el-select v-model="ruleForm.pro_manager"
                         class="normal-input"
                         placeholder="选择项目经理">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质量负责人"
                          prop="quality_manager">
              <el-select v-model="ruleForm.quality_manager"
                         class="normal-input"
                         placeholder="选择质量负责人">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产负责人1"
                          prop="production_manager">
              <el-select v-model="ruleForm.production_manager"
                         class="normal-input"
                         placeholder="选择生产负责人1">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产负责人2"
                          prop="production_manager_two">
              <el-select v-model="ruleForm.production_manager_two"
                         class="normal-input"
                         placeholder="选择生产负责人2">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产负责人3"
                          prop="production_manager_three">
              <el-select v-model="ruleForm.production_manager_three"
                         class="normal-input"
                         placeholder="选择生产负责人3">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="吊装负责人"
                          prop="charge_manager">
              <el-select v-model="ruleForm.charge_manager"
                         class="normal-input"
                         placeholder="选择吊装负责人">
                <el-option v-for="item in buildList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="详细地址"
                          prop="detail_address">
              <el-input class="normal-input"
                        v-model="ruleForm.detail_address"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建筑面积"
                          prop="build_area">
              <el-input class="normal-input"
                        v-model="ruleForm.build_area"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目效果图"
                          prop="detail_address">
              <el-upload class="avatar-uploader"
                         action="https://jsonplaceholder.typicode.com/posts/"
                         :show-file-list="false"
                         :on-success="handleAvatarSuccess"
                         :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl"
                     :src="imageUrl"
                     class="avatar">
                <i v-else
                   class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="visible = false"
                   type="primary">保 存</el-button>
        <el-button @click="dialogClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    var checkProName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入项目名称'))
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('项目名称首尾不能含有空格！'))
      } else {
        callback()
      }
    }
    var checkProNickName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入项目简称'))
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('项目简称首尾不能含有空格！'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      ruleForm: {
        project_name: '',
        nick_name: '',
        business_type: '',
        system_num: '',
        pro_state: '',
        pro_num: '',
        profess_type: '',
        build_type: '',
        deal_total: '',
        pro_manager: '',
        quality_manager: '',
        production_manager: '',
        production_manager_two: '',
        production_manager_three: '',
        charge_manager: '',
        detail_address: '',
        build_area: ''
      },
      rules: {
        project_name: [
          { required: true, validator: checkProName, trigger: 'blur' }
        ],
        nick_name: [
          { required: true, validator: checkProNickName, trigger: 'blur' }
        ],
        business_type: [
          { required: true, message: '请选择业务类型', trigger: 'change' }
        ],
        pro_state: [
          { required: true, message: '请选择项目状态', trigger: 'change' }
        ],
        profess_type: [
          { required: true, message: '请选择专业类型', trigger: 'change' }
        ],
        build_type: [
          { required: true, message: '请选择建筑类型', trigger: 'change' }
        ],
        detail_address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      typeList: [],
      projectList: [],
      professList: [
        {
          value: '1',
          label: '建设单位'
        }, {
          value: '2',
          label: '检测单位'
        }
      ],
      buildList: [],
      buildUnitArr: [
        {
          build_unit: ''
        }
      ],
      imageUrl: ''
    }
  },
  props: {
    dialogVisible: Boolean,
    dialogForm: Object
  },
  watch: {
    dialogVisible (newval, oldval) {
      this.visible = newval
    },
    dialogForm: {
      handler (newval, oldval) {
        this.ruleForm = JSON.parse(JSON.stringify(newval))
      },
      deep: true
    }
  },
  methods: {
    // 添加单位功能
    addUnit () {
      this.buildUnitArr.push({ build_unit: '' })
    },
    // 删除单位功能
    deleteUnit (index) {
      this.buildUnitArr.splice(index, 1)
    },
    // 图片上传功能
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 图片上传前的钩子
    beforeAvatarUpload (file) {

    },
    dialogClose () {
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.editProject-form {
  .build-unit {
    margin-bottom: 12px;
  }
  .normal-input {
    width: 95%;
  }
  .add-input {
    width: 72% !important;
  }
  .add-btn {
    width: 22% !important;
  }
  .area-input {
    width: 32%;
  }
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
/deep/ .dialog-footer {
  text-align: center;
}
.edit-project-container {
}
</style>
